<?php

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_talkedit extends base_control{

	public function __construct() {

		parent::__construct();

	}

	public function do_index() {
		$this->check_user_login_msg();
		$tid = core::gpc('tid','G');
		$content = core::gpc('content','P');
		$title = core::gpc('title','P');
		
		$the_talk = $this->talks->talks_show($tid);
		if(empty($the_talk)) {
			$this->message('帖子不存在', 'index.php');
		}
		if($the_talk['uid'] != $this->user->user_uid()) {
			$this->message('你没有这个权限', 'index.php');
		}
		if(empty($title) || empty($content)) {
			$title = $the_talk['title'];
			$content = $the_talk['content'];
		}else{
			$this->parse->parse_title($title);
			$this->parse->parse_content($content, false);
		}
		
		$talksub = core::gpc('talksub', 'P');
		$addface = core::gpc('addface', 'P');
		$addfile = core::gpc('addfile', 'P');
		$addimgs = core::gpc('addimgs', 'P');
		
		$show_face_html = false;
		$show_file_html = false;
		$show_imgs_html = false;
		$show_face_num = 1;
		$alert_msg = '';
		$face_name = $this->parse->parse_facename();
		$done = 0;
		if($addface != NULL) $done += 1;
		if($addfile != NULL) $done += 10;
		if($addimgs != NULL) $done += 50;
		if($talksub != NULL) $done += 100;
		
		switch($done) {
		case 1:
			$facecode = core::gpc('facecode', 'P');
			if(empty($facecode)) {
				$show_face_html = true;
			}else{
				$content .= "{JYFACE:$facecode}";
			}
			break;
		case 10:
			$show_file_html = true;
			if(html::form_submit( CONFIG_CS_KEY )) {
				$this->upfile->upload();
				if($this->upfile->error_code == 0) {
					$uid = $this->this_user->user_uid();
					$md5 = md5($this->upfile->upload_path);
					$upinfo = array();
					$upinfo['fname'] = utf8::safe_substr($this->upfile->file_info['name'], 0, 60);
					$upinfo['fsize'] = abs(filesize($this->upfile->upload_path));
					if($this->attach->attach_add($uid,$this->upfile->upload_path,
							$md5, core::json_encode($upinfo) )){
						$show_file_html = false;
						$content .= "{JYFILE:$md5}";
					}else{
						@unlink($this->upfile->upload_path);
						$alert_msg = '保存附件失败';
					}
				}else{
					$alert_msg = $this->miscfunc->check_upload_err($this->upfile->error_code);
				}
			}else{
				$alert_msg = '附件大小不能超过'.misc::humansize($this->confs['_file_allow_size'])
								.'<br/>允许格式：';
				foreach($this->confs['_file_file_ext'] as $k=>$v)
					$alert_msg .= ".$v ";
			}
			break;
		case 50:
			$show_imgs_html = true;
			if(html::form_submit( CONFIG_CS_KEY )) {
				$this->upimg->upload();
				if($this->upimg->error_code == 0) {
					$image_size = getimagesize($this->upimg->upload_path);
					if($image_size == false) {
						@unlink($this->upimg->upload_path);
						$alert_msg = '图像格式及签名不正确';
					}
					if($alert_msg == '' && $image_size[0]*$image_size[1] > 1000000) {
						@unlink($this->upimg->upload_path);
						$alert_msg = '分辨率不能超过1000*1000';
					}
					if($alert_msg == '' && ($image_size[0]>300 || $image_size[1]>300)) {
						$thum_name = $this->parse->parse_thumb_name($this->upimg->upload_path);
						$this->rzimg->resize_img($this->upimg->upload_path, 300, 300, 1,
										CONFIG_THU_PATH, $thum_name);
						if(!file_exists($this->rzimg->result_img)) {
							@unlink($this->upimg->upload_path);
							$alert_msg = '生成缩略图失败';
						}
					}
					if($alert_msg == '') {
						$uid = $this->this_user->user_uid();
						$md5 = md5($this->upimg->upload_path);
						$upinfo = array();
						$upinfo['fname'] = utf8::safe_substr($this->upimg->file_info['name'], 0, 60);
						$upinfo['fsize'] = abs(filesize($this->upimg->upload_path));
						if($this->attach->attach_add($uid,$this->upimg->upload_path,
								$md5, core::json_encode($upinfo) )){
							$show_imgs_html = false;
							$content .= "{JYIMGS:$md5}";
						}else{
							@unlink($this->upimg->upload_path);
							$alert_msg = '保存图片失败';
						}
					}
				}else{
					$alert_msg = $this->miscfunc->check_upload_err($this->upimg->error_code);
				}
			}else{
				$alert_msg = '图片大小不能超过'.misc::humansize($this->confs['_file_allow_size'])
				.'<br/>分辨率不能超过1000*1000'
				.'<br/>允许格式：';
				foreach($this->confs['_file_imgs_ext'] as $k=>$v)
					$alert_msg .= ".$v ";
			}
			break;
		case 100:
			$attaches1_new = $this->parse->parse_all_image_md5($content);
			$attaches2_new = $this->parse->parse_all_files_md5($content);
			$attaches1_old = $this->parse->parse_all_image_md5($the_talk['content']);
			$attaches2_old = $this->parse->parse_all_files_md5($the_talk['content']);
			$attache_new = array_merge($attaches1_new, $attaches2_new);
			$attache_old = array_merge($attaches1_old, $attaches2_old);
			
			foreach($attache_new as $k=>$v) {
				if(!in_array($v, $attache_old))
					$this->attach->attach_edit($v, $tid);
			}
			foreach($attache_old as $k=>$v) {
				if(!in_array($v, $attache_new))
					$this->attach->attach_edit($v, 0);
			}
			
			if($this->talks->talks_edit($tid , -1, $title, $content)) {
				$this->message('修改成功', "index.php?index-talk-tid-$tid.htm");
			}else{
				$this->message('修改失败', "index.php?index-talk-tid-$tid.htm");
			}
			break;
		default:
			break;
		}
		
		$FORM_HASH = html::form_hash( CONFIG_CS_KEY );
		
		$this->tpl->assign('FORM_HASH', $FORM_HASH);
		$this->tpl->assign('tid', $tid);
		$this->tpl->assign('content', $content);
		$this->tpl->assign('title', $title);
		$this->tpl->assign('show_face_html', $show_face_html);
		$this->tpl->assign('show_file_html', $show_file_html);
		$this->tpl->assign('show_imgs_html', $show_imgs_html);
		$this->tpl->assign('face_name', $face_name);
		$this->tpl->assign('alert_msg', $alert_msg);
		$this->tpl->assign('show_face_num', $show_face_num);
		$this->tpl->display('talkedit_set.tpl');
	}

}
/*{TranslateTagEnd}*/

?>